package com.originalpoint.origin_server.controller;

import com.originalpoint.origin_server.pojo.Login;
import com.originalpoint.origin_server.result.LoginResult;
import com.originalpoint.origin_server.service.LoginService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.management.relation.Role;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class LoginController {
    @Autowired
    private LoginService loginService;

    @ApiOperation(value  = "登录时判断用户名和密码是否正确")
    @PostMapping("/login")
    public Map login(@RequestBody Map<String, Object> request) {
        String username = (String) request.get("loginName");
        String password = (String) request.get("password");
        int role = (int) request.get("role");
        Login login = new Login();
        login.setRole(role);
        login.setUsername(username);
        login.setPassword(password);
        LoginResult result = loginService.login(login);
        System.out.println(result);
        System.out.println(role);

         //将用户ID放入返回结果中
        Map<String, Object> response = new HashMap<>();
//        if(role!=result.getRole()){
//            response.put("typeerror","typeerror");
//        }else {
//            response.put("typeerror","right");
//        }
        response.put("success", result.isSuccess());
        response.put("message", result.getMessage());
        response.put("userId", result.getUserId()); // 添加userId字段
        return response;
    }
}
